package com.example.memory.servicde.impl;

import com.example.memory.base.service.impl.BaseServiceImpl;
import com.example.memory.entity.File;
import com.example.memory.servicde.FileService;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

@Service
public class FileServiceImpl extends BaseServiceImpl<File> implements FileService {

    @Override
    public void getResource(HttpServletRequest request, HttpServletResponse response) throws IOException {
        File file = select(request.getParameter("id"));
        ServletOutputStream outputStream = response.getOutputStream();
        StreamUtils.copy(Files.newInputStream(Paths.get(file.getPath(), file.getName())), outputStream);
        outputStream.flush();
        outputStream.close();
    }
}
